//
//  RegisterViewController.m
//  OrangeNote
//
//  Created by Vandals on 16/1/22.
//  Copyright © 2016年 崇龙. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginButton.h"
#import "StTransitions.h"
#import "LoginViewController.h"
#import "UserDataManager.h"
#import "UserModel.h"

#define kWidhth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height


@interface RegisterViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField *password2;
@property(nonatomic,strong)UIScrollView *scrollView;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaotilan"]];

    
    [self addSubViews];
    
}

-(void)addSubViews{
    
    
    
    UIImageView *imv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv.frame = CGRectMake(0, 0, kWidhth, kHeight);
    
    UIImageView *imv1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv1.frame = CGRectMake(0, kHeight, kWidhth, kHeight);
    UIImageView *imv2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv2.frame = CGRectMake(0, kHeight*2, kWidhth, kHeight);
    UIImageView *imv3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv3.frame = CGRectMake(0, kHeight*3, kWidhth, kHeight);
    UIImageView *imv4 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv4.frame = CGRectMake(0, kHeight*4,kWidhth, kHeight);
    UIImageView *imv5 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv5.frame = CGRectMake(0, kHeight*5, kWidhth, kHeight);
    UIImageView *imv6 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv6.frame = CGRectMake(0, kHeight*6, kWidhth, kHeight);
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.frame = CGRectMake(0, 0, kWidhth, kHeight);
    self.scrollView.contentSize = CGSizeMake(kWidhth, kHeight*7);
//    self.scrollView.alwaysBounceVertical = YES;//垂直
    self.scrollView.scrollEnabled = NO;
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:imv];
    [self.scrollView addSubview:imv1];
    [self.scrollView addSubview:imv2];
    [self.scrollView addSubview:imv3];
    [self.scrollView addSubview:imv4];
    [self.scrollView addSubview:imv5];
    [self.scrollView addSubview:imv6];
    
    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];

    
    
    // 用户名Label
    _username = [[UITextField alloc]initWithFrame:CGRectMake(5, kHeight/2 - 250, kWidhth-10, 60)];
    _username.delegate = self;
    _username.placeholder = @"请输入用户名";
    _username.textAlignment = NSTextAlignmentCenter;
    [_username setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_username setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    _username.layer.masksToBounds = YES;
    _username.layer.cornerRadius = 10;
    _username.layer.borderColor = [[UIColor orangeColor]CGColor];
    _username.layer.borderWidth = 1.0;
    _username.backgroundColor = [UIColor lightTextColor];
    _username.alpha = 0.8;
    _username.textColor = [UIColor redColor];
    _username.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:_username];
    
    // 密码Lbael
    _password = [[UITextField alloc]initWithFrame:CGRectMake(5, kHeight/2 - 180, kWidhth-10, 60)];
    _password.delegate = self;
    _password.placeholder = @"请输入密码";
    _password.secureTextEntry = YES;
    _password.textColor = [UIColor redColor];
    _password.textAlignment = NSTextAlignmentCenter;
    [_password setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_password setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    _password.layer.masksToBounds = YES;
    _password.layer.cornerRadius = 10;
    _password.layer.borderWidth = 1.0;
    _password.layer.borderColor = [[UIColor orangeColor]CGColor];
    _password.backgroundColor = [UIColor lightTextColor];
    _password.alpha = 0.8;
    _password.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:_password];
    
    // 第二次输入密码
    _password2 = [[UITextField alloc]initWithFrame:CGRectMake(5, kHeight/2 - 110, kWidhth-10, 60)];
    _password2.delegate = self;
    _password2.placeholder = @"请再次输入密码";
    _password2.secureTextEntry = YES;
    _password2.textColor = [UIColor redColor];
    _password2.textAlignment = NSTextAlignmentCenter;
    [_password2 setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_password2 setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    _password2.layer.masksToBounds = YES;
    _password2.layer.cornerRadius = 10;
    _password2.layer.borderWidth = 1.0;
    _password2.layer.borderColor = [[UIColor orangeColor]CGColor];
    _password2.backgroundColor = [UIColor lightTextColor];
    _password2.alpha = 0.8;
    _password2.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:_password2];
    
    // 邮箱
    _email = [[UITextField alloc]initWithFrame:CGRectMake(5, kHeight/2 - 40, kWidhth-10, 60)];
    _email.delegate = self;
    _email.placeholder = @"请输入邮箱";
    _email.textColor = [UIColor redColor];
    _email.textAlignment = NSTextAlignmentCenter;
    [_email setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_email setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    _email.layer.masksToBounds = YES;
    _email.layer.cornerRadius = 10;
    _email.layer.borderWidth = 1.0;
    _email.layer.borderColor = [[UIColor orangeColor]CGColor];
    _email.backgroundColor = [UIColor lightTextColor];
    _email.alpha = 0.8;
    _email.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:_email];
    
    
    // 注册按钮
    LoginButton *reggBTN = [[LoginButton alloc]initWithFrame:CGRectMake(kWidhth/2 - 150, kHeight/2+30, 300, 60)];
    reggBTN.backgroundColor = [UIColor orangeColor];
    reggBTN.alpha = 0.7;
    [self.view addSubview:reggBTN];
    
    [reggBTN setTitle:@"注册" forState:UIControlStateNormal];
    [reggBTN addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    
    // 返回登陆按钮
    UIButton *backing = [[UIButton alloc]initWithFrame:CGRectMake(kWidhth/2 - 150, kHeight/2+100, 300, 60)];
    backing.backgroundColor = [UIColor orangeColor];
    backing.layer.cornerRadius = 30;
    backing.alpha = 0.7;
    [backing setTitle:@"返回" forState:UIControlStateNormal];
    [backing addTarget:self action:@selector(backing:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backing];
    

    
}

////滚图的动画效果
//-(void)pageTurn:(UIPageControl *)aPageControl{
//    int whichPage = aPageControl.currentPage;
//    [UIView beginAnimations:nil context:NULL];
//    [UIView setAnimationDuration:0.3f];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    [self.scrollView setContentOffset:CGPointMake(320.0f * whichPage, 0.0f) animated:YES];
//    [UIView commitAnimations];
//}

// timer轮播方法
CGFloat myNum = 0;
-(void)scrollTimer{
    myNum += 0.5;
    _scrollView.contentOffset = CGPointMake(0, myNum);
    if (myNum == kHeight*7) {
        _scrollView.contentOffset = CGPointMake(0, 0);
        myNum = 0;
    }
    
}












- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
//    CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5));
}
// 注册按钮方法
-(void)registerUser:(LoginButton *)registering{
    
    // 模拟网路访问
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self regisBtnAction:registering];
    });
}
-(void)regisBtnAction:(LoginButton *)registering{
    
    if (_username.text == nil || _username.text == NULL ||[_username.text isEqualToString:@""]) {
        // 长按动画还原
        [registering ErrorRevertAnimationCompletion:nil];
        // 延迟
        [self performSelector:@selector(userNameErrorAction) withObject:nil afterDelay:1.0];
    }else if (_password.text == nil || _password.text == NULL || [_password.text isEqualToString:@""]){
        // 长按动画还原
        [registering ErrorRevertAnimationCompletion:nil];
        // 延迟
        [self performSelector:@selector(passwordErrorAction) withObject:nil afterDelay:1.0];
    }else if (_password2.text == nil || _password2.text == NULL ||[_password2.text isEqualToString:@""]){
        // 长按动画还原
        [registering ErrorRevertAnimationCompletion:nil];
        // 延迟
        [self performSelector:@selector(passwordErrorAction) withObject:nil afterDelay:1.0];
    }else if ([_password.text isEqualToString:_password2.text] == NO){
        // 长按动画还原
        [registering ErrorRevertAnimationCompletion:nil];
        // 延迟
        [self performSelector:@selector(passwordNotError) withObject:nil afterDelay:1.0];
    }else{
        
        
        UserModel *userModel = [[UserModel alloc]init];
        userModel.username = _username.text;
        userModel.password = _password.text;
        userModel.email = _email.text;
        
        UserDataManager *manager = [[UserDataManager alloc]init];
        [manager userRigstWithUserModel:userModel];
        
        manager.registSuccessBlock = ^(){
            [registering ExitAnimationCompletion:^{
                [self didPresentControllerButtonTouch];
            }];
        };
        
        manager.registFailedBlock = ^(NSString *message){
            [registering ErrorRevertAnimationCompletion:nil];
            [self registErrorAlert:message];
            
        };
        
    }
}

- (void)registErrorAlert:(NSString *)message{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertC addAction:btnAction];
    [self presentViewController:alertC animated:YES completion:nil];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_username resignFirstResponder];
    [_password resignFirstResponder];
    [_password2 resignFirstResponder];
    [_email resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_username resignFirstResponder];
    [_password resignFirstResponder];
    [_password2 resignFirstResponder];
    [_email resignFirstResponder];
    return YES;
}
// 用户名为空时执行的方法
-(void)userNameErrorAction{
    UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertCtrl addAction:btnAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
}
// 密码为空时执行的方法
-(void)passwordErrorAction{
    UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertCtrl addAction:btnAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
    
}
// 两次密码输入不一致时
-(void)passwordNotError{
    UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不一致!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertCtrl addAction:btnAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
}
// 返回按钮方法
-(void)backing:(UIButton *)back{
    [self.navigationController popViewControllerAnimated:YES];
}

// 注册成功跳转方法
-(void)didPresentControllerButtonTouch{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    return [[StTransitions alloc]initWithTransitionDuration:0.4f StartingAlpha:0.5f isBOOL:true];
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    
    return [[StTransitions alloc]initWithTransitionDuration:0.4f StartingAlpha:0.8f isBOOL:false];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
